package com.woniu.woniu_bx.controller;

import com.woniu.woniu_bx.pojo.Goods;
import com.woniu.woniu_bx.pojo.GoodsCategory;
import com.woniu.woniu_bx.pojo.PageBean;
import com.woniu.woniu_bx.pojo.ResponseEntity;
import com.woniu.woniu_bx.service.GoodsCategoryService;
import com.woniu.woniu_bx.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName GoodsCategoryController
 * @Description TODO
 * @Author 于文龙
 * @Date 2022/8/29 11:05
 * @Version 1.0
 **/
@RestController
public class GoodsCategoryController {
    @Autowired
    GoodsCategoryService goodsCategoryService;
    @Autowired
    GoodsService goodsService;

    //分页展示商品类别
    @GetMapping("/admin/category/show/{pageNum}/{sizeNum}")
    @PreAuthorize("hasAnyAuthority('goodsCate:list')")
    public ResponseEntity<PageBean> show(@PathVariable(name="pageNum") int pageNum, @PathVariable(name="sizeNum") int sizeNum){
        PageBean<GoodsCategory> page = goodsCategoryService.getByPage(pageNum, sizeNum);
        if(page!=null){
            return ResponseEntity.SUCCESS("查询成功",page);
        }else{
         return ResponseEntity.FAIL("查询失败",null);
        }
    }

    //添加商品类别
    @PostMapping("/admin/category/addCategory")
    @PreAuthorize("hasAnyAuthority('goodsCate:add')")
    public ResponseEntity<GoodsCategory> add(@RequestBody GoodsCategory goodsCategory){
        GoodsCategory byOrderNumber = goodsCategoryService.findByOrderNumber(goodsCategory.getOrderNumber());
        GoodsCategory goodsCategory1=goodsCategoryService.findByName(goodsCategory.getName());
        if(byOrderNumber!=null||goodsCategory1!=null){
            return ResponseEntity.FAIL("序号或名称已经存在",null);
        }else {
            boolean flag = goodsCategoryService.add(goodsCategory);
            if (flag) {
                return ResponseEntity.SUCCESS("添加成功", goodsCategory);
            } else {
                return ResponseEntity.FAIL("添加失败", null);
            }
        }
    }

    //修改商品类别信息（交换序号变更位置也是这个方法）
    @PostMapping("/admin/category/update")
    @PreAuthorize("hasAnyAuthority('goodsCate:add')")
    public ResponseEntity<GoodsCategory> updateCategory(@RequestBody GoodsCategory goodsCategory){
        GoodsCategory oldGoodsCategory= goodsCategoryService.getById(goodsCategory.getId());
        if(oldGoodsCategory.getOrderNumber()!=goodsCategory.getOrderNumber()){
            GoodsCategory oldgoodsCategoryOrderNumOwn = goodsCategoryService.findByOrderNumber(goodsCategory.getOrderNumber());
            if(oldgoodsCategoryOrderNumOwn!=null){
                goodsCategoryService.changeorderNumber(goodsCategory.getId(),oldgoodsCategoryOrderNumOwn.getId());
            }
        }
        boolean flag = goodsCategoryService.update(goodsCategory);
        if(flag){
            return ResponseEntity.SUCCESS("修改成功",goodsCategory);
        }else{
            return ResponseEntity.FAIL("商品已存在",null);
        }
    }

    //删除商品类别
    @GetMapping("/admin/category/del/{id}")
    @PreAuthorize("hasAnyAuthority('goodsCate:del')")
    public ResponseEntity<String> del(@PathVariable(name="id")int id){
        boolean flag = goodsCategoryService.deleteById(id);
        if(flag){
            return ResponseEntity.SUCCESS("删除成功",null);
        }else{
            return ResponseEntity.FAIL("删除失败",null);
        }
    }
    //模糊查询
    @PostMapping("/admin/category/fuzzy")
    @PreAuthorize("hasAnyAuthority('goodsCate:list')")
    public ResponseEntity<List<GoodsCategory>> fuzzyFind(@RequestBody GoodsCategory goodsCategory){
        if(goodsCategory.getName()==""&&goodsCategory.getStatus()==""&&goodsCategory.getNav()==""){
            return null;
        }
        List<GoodsCategory> categories = goodsCategoryService.selectByName(goodsCategory.getName(),goodsCategory.getNav(),goodsCategory.getStatus());
        return ResponseEntity.SUCCESS("模糊查询成功",categories);
    }

    //锁定
    @GetMapping("/admin/category/lock/{id}")
    @PreAuthorize("hasAnyAuthority('goodsCate:lock')")
    public ResponseEntity<String> lock(@PathVariable(name="id")int id){
        goodsCategoryService.lock(id);
        return  ResponseEntity.SUCCESS("锁定成功",null);
    }
    //解锁
    @GetMapping("/admin/category/unlock/{id}")
    @PreAuthorize("hasAnyAuthority('goodsCate:unlock')")
    public ResponseEntity<String> unlock(@PathVariable(name="id")int id){
        goodsCategoryService.unlock(id);
        return  ResponseEntity.SUCCESS("锁定成功",null);
    }
    //名称检查
    @PostMapping("/admin/category/checkName")
    public ResponseEntity<String> checkName(@RequestBody GoodsCategory goodsCategory){
        if(goodsCategory.getOrderNumber()==0&&goodsCategory.getName()==null){
            return ResponseEntity.SUCCESS(null,null);
        }else if(goodsCategoryService.findByOrderNumber(goodsCategory.getOrderNumber())!=null||goodsCategoryService.findByName(goodsCategory.getName())!=null){
            return ResponseEntity.FAIL(null,null);
        }
        return ResponseEntity.SUCCESS(null,null);
    }
    //根据序号查询
    @GetMapping("/category/byordernum")
    public ResponseEntity<List<GoodsCategory>> findByOrderNum(){
        List<GoodsCategory> categories = goodsCategoryService.selectByOrderNumberReception();
        return ResponseEntity.SUCCESS("导航查询成功",categories);
    }

    //根据类别查找商品(未使用)
    @GetMapping("/bycategoryid")
    public ResponseEntity<List<Goods>> findGoodsByCategoryId(int id){
        List<Goods> goods = goodsCategoryService.findByCategoryId(id);
        return ResponseEntity.SUCCESS("按类别查询成功",goods);
    }

    //随机查询三条陪玩商品信息
    @GetMapping("/category/random")
    public ResponseEntity<List<Goods>> findByRandom(){
        List<Goods> byRandom = goodsService.findByRandom();
        return ResponseEntity.SUCCESS("随机查询成功",byRandom);
    }
}
